<?php

namespace app\index\controller\tan;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
 * 首页
 */
class Index extends Controller
{
    public function x(){
//        $token = $this->request->token('__token__', 'sha1');
        $token = $this->request->token();
        echo $token;
        
        
    }

    /**
     *首页统计
     * @return \think\Response
     */
    public function index()
    {
//        Session::set("user.id",1);
        $data=Db::table("agent")
                ->where([
                    "user_id"=> Session::get("user.id")
                ])->field([
                    "sum(attention) as attention", #关注收藏数
                    "sum(sales) sales",#累计销售单数
                    "sum(sales_amount) sales_amount",#累计销售金额
                    "sum(product_number) product_number",#在售产品数量
                ])->cache()->find();
        $data["count"]=Db::table("product")->where([
                            "business_id"=> Session::get("user.id"),
                            "is_shelf"=>3
                        ])->cache()->count();#在售商品数量
               $data['daiban']= Db::table("orders_info")
                       ->alias("i")
                       ->join("orders o","o.batchcode=i.batchcode")
                        ->where([
                            "i.business_id"=> Session::get("user.id"),
                            "o.status"=>["in",[0,4]]
                        ])->cache()->count();#待办
                $d=Db::table("orders_info")
                       ->alias("i")
                       ->join("orders o","o.batchcode=i.batchcode")
                        ->where([
                            "i.business_id"=> Session::get("user.id"),
                            "o.status"=>["in",[0,4]]
                        ])-> field([
                            "count(*) as count", #退单数量
                            "sum(o.total_price) as sum" #退单总金额
                        ])->cache()->find();#代办
                $data['tuidan_count']=$d["count"];
                $data['tuidan_sum']=$d["sum"];
                $data['xianxia_product_count']=Db::table("orders_info")
                       ->alias("i")
                       ->join("orders o","o.batchcode=i.batchcode")
                       ->where([
                           "i.business_id"=> Session::get("user.id"),
                           "o.type"=>0
                       ])
                       ->cache()->count();#线下支付订单数
                $data['xianshang_product_count']=Db::table("orders_info")
                       ->alias("i")
                       ->join("orders o","o.batchcode=i.batchcode")
                       ->where([
                           "i.business_id"=> Session::get("user.id"),
                           "o.type"=>1
                       ])
                       ->cache()->count();#线下支付订单数
                
                
        print_r($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
